package Salesman;

import Salesman.Data.DataProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.LinkedList;
import static Salesman.ArrayUtils.*;

public class Distances implements Comparable<Distances> {

    private double[][] matrix;
    private double minRemaining;
    private List<Integer> route;
    private List<Integer> unused;

    public Distances(int n) {
        this(new double[n][n]);
    }

    public Distances(double[][] matrix) {
        this.matrix = matrix;
        calculateMinRemaining();
        route = new LinkedList<Integer>();
        unused = new LinkedList<Integer>();
        for (int i = 0; i < matrix.length; i++) {
            if (DataProvider.getCityData().get(i).isInSimulation()) {
                unused.add(i);
            }
        }
    }

    public double get(int from, int to) {
        return matrix[from][to];
    }

    public void set(int from, int to, double dist) {
        matrix[from][to] = dist;
    }

    public void setSynched(int from, int to, double dist) {
        matrix[to][from] = dist;
        matrix[from][to] = dist;
    }

    public void fillAll(double dist) {
        double[] da = new double[matrix.length];
        Arrays.fill(da, dist);
        Arrays.fill(matrix, da);
    }

    public void fillDiag(double dist) {
        for (int i = 0; i < matrix.length; i++) {
            matrix[i][i] = dist;
        }
    }

    private void calculateMinRemaining() {
        double[] minc = new double[matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            minc[i] = minOf(matrix[i]);
        }

        double[] minr = new double[matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            minr[i] = minOf(minus(colOf(matrix, i), minc));
        }

        minRemaining = sumOf(concat(minc, minr));
    }

    public double minRemaining() {
        return minRemaining;
    }

    public double[][] data() {
        return copyaad(matrix);
    }

    public Distances copy() {
        Distances d = new Distances(copyaad(matrix));
        d.setRoute(route);
        d.setUnused(unused);
        return d;
    }

    public Distances visit(int to) {
        if (route.contains(new Integer(to))) {
            return null;
        }

        double[][] m = copyaad(matrix);

        if (!route.isEmpty()) {
            int from = getRoute().get(getRoute().size() - 1);
            for (int i = 0; i < m.length; i++) {
                if (i != from) {
                    m[i][to] = Double.POSITIVE_INFINITY;
                }
                if (i != to) {
                    m[from][i] = Double.POSITIVE_INFINITY;
                }
            }
            m[to][from] = Double.POSITIVE_INFINITY;
        }

        Distances d = new Distances(m);
        d.setRoute(route);
        d.getRoute().add(to);
        d.setUnused(unused);
        d.getUnused().remove(new Integer(to));
        return d;
    }

    @Override
    public int compareTo(Distances o) {
        return Double.compare(minRemaining(), o.minRemaining());
    }

    public List<Integer> getRoute() {
        return route;
    }

    public void setRoute(List<Integer> route) {
        this.route = new ArrayList(route);
    }

    public List<Integer> getUnused() {
        return unused;
    }

    public void setUnused(List<Integer> unused) {
        this.unused = new ArrayList(unused);
    }

    public String routeToString() {
        String s = "";
        if (!route.isEmpty()) {
            s += route.get(0).toString();
        }

        if (route.size() > 1) {
            for (int i = 1; i < route.size(); i++) {
                s += "->" + route.get(i).toString();
            }
        }
        return s;
    }

    public String unusedToString() {
        String s = "{";
        for (int i = 0; i < unused.size(); i++) {
            if (i > 0) {
                s += ", ";
            }
            s += unused.get(i).toString();
        }
        return s + "}";
    }
}
